package com.android.questions.ui.dialog;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DisplayUtil;
import com.android.questions.R;
import com.android.questions.databinding.DialogPushExtraBinding;

/**
 * FileName: DialogPushExtra
 * Author: admin
 * Date: 2021/11/16 16:05
 * Description:
 */
public class DialogPushExtra extends Dialog implements View.OnClickListener {

    private DialogPushExtraBinding pushExtraBinding;
    private final Activity activity;
    private OnClickLister clickLister;
    private boolean canReward; // 是否可以领取

    public DialogPushExtra(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pushExtraBinding = DialogPushExtraBinding.inflate(activity.getLayoutInflater());
        setContentView(pushExtraBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initView();
        initLister();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        pushExtraBinding.lavGift1.setVisibility(View.VISIBLE);
        pushExtraBinding.lavGift2.setVisibility(View.INVISIBLE);

        String s = "最高可领取<b><font color='#FD0D21'>" + 10 + "</font></b>元";
        pushExtraBinding.tvTips.setText(Html.fromHtml(s));

        if (canReward) {
            pushExtraBinding.ivReceive.setImageResource(R.drawable.icon_push_receive);
            pushExtraBinding.ivReceive.setEnabled(true);
        } else {
            pushExtraBinding.ivReceive.setEnabled(false);
        }
    }

    private void initLister() {
        pushExtraBinding.ivReceive.setOnClickListener(this);
        pushExtraBinding.ivClose.setOnClickListener(this);
    }

    public DialogPushExtra setClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        if (v.getId() == R.id.iv_receive) {
            pushExtraBinding.ivReceive.setEnabled(false);
            pushExtraBinding.lavGift1.setVisibility(View.INVISIBLE);
            pushExtraBinding.lavGift2.setVisibility(View.VISIBLE);
            pushExtraBinding.lavGift2.playAnimation();
            pushExtraBinding.lavGift2.addAnimatorListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {

                }

                @Override
                public void onAnimationEnd(Animator animator) {
                    dismiss();
                    if (clickLister != null) {
                        clickLister.onReceive();
                    }
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            });
        } else {
            dismiss();
        }
    }

    public DialogPushExtra setCanReward(boolean canReward) {
        this.canReward = canReward;
        return this;
    }

    public interface OnClickLister {
        void onReceive();
    }
}